package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.exception.REnum;
import cn.edu.ctbu.datajpa01.exception.RException;
import cn.edu.ctbu.datajpa01.service.StudentService;
import cn.edu.ctbu.datajpa01.service.impl.StudentImplService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    StudentImplService studentImplService;

    @GetMapping("user")
    public int user(HttpServletRequest httprequest,String username,String password)throws Exception {

        //判断用户名和密码是否存在
        Student students=studentImplService.findByNameAndPassword(username,password);
        if (students!=null) {

            HttpSession session = httprequest.getSession();
            session.setAttribute("userid", students.getId());

            session.setAttribute("user", students);

            return 1;//成功
        } else {
//如果不存在，返回出错
//            return -1;//不存在用户
            throw new RException(REnum.LOGIN_ERR);
        }
    }

}



